﻿
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;
using System.Collections;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.IO;
using System.Windows.Markup;
using System.Reactive;
using System.Reactive.Linq;
using System.Collections.ObjectModel;
#endregion

namespace FasterWPF
{
    public static class TextBoxBaseExt
    {
        //NOTE: TextBoxBase IS NOT AN IPARENT. IT IS ONLY AN ISCROLLVIEWERSTRATEGY

        #region Core TextBoxBase-Specific Construction Methods

        public static TextBoxBase InitializeForScrollViewerSupport(this TextBoxBase source)
        {
            return InitializeForScrollViewerSupport(source, double.NaN, double.NaN, null); //Default width and height to Auto by setting to NaN
        }

        public static TextBoxBase InitializeForScrollViewerSupport(this TextBoxBase source, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return InitializeForScrollViewerSupport(source, double.NaN, double.NaN, null); //Default width and height to Auto by setting to NaN
        }

        public static TextBoxBase InitializeForScrollViewerSupport(this TextBoxBase source, double width, double height, double? minHeight, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return InitializeForScrollViewerSupport(source, width, height, minHeight); //Default width and height to Auto by setting to NaN
        }

        public static TextBoxBase InitializeForScrollViewerSupport(this TextBoxBase source, double width, double height, double? minHeight)
        {
            //Force the Aero theme by default to assure the structure of the TextBoxBase
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            if (minHeight != null)
            {
                source.MinHeight = (double)minHeight;
            }

            if (source is TextBox)
            {
                (source as TextBox).TextWrapping = TextWrapping.Wrap;
            }
            
            source.AcceptsReturn = true;
            source.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;          

            TextBoxBaseScrollViewerStrategy lbsr = new TextBoxBaseScrollViewerStrategy();
            source.StoreSetting<TextBoxBase>("IScrollViewerStrategy", lbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(0, 0, 0, 1)); //default Border     
            source.StoreSetting<TextBoxBase>("BorderSettings", new BorderSettings(){ Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness= new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) } );
            
            //source.StoreSetting<TextBoxBase>("ScrollViewerDeltaStep", 1);
            //source.StoreSetting<TextBoxBase>("HorizScrollViewerDeltaStep", 1);

            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollDownStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollDownDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollDownInterval", 100);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageDownStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageDownDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageDownInterval", 100);

            //source.StoreSetting<TextBoxBase>("ScrollViewerPageUpStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageUpDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageUpInterval", 100);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollUpStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollUpDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollUpInterval", 100);

            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollRightStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollRightDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollRightInterval", 100);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageRightStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageRightDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageRightInterval", 100);

            //source.StoreSetting<TextBoxBase>("ScrollViewerPageLeftStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageLeftDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerPageLeftInterval", 100);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollLeftStep", 1);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollLeftDelay", 1000);
            //source.StoreSetting<TextBoxBase>("ScrollViewerScrollLeftInterval", 100);
            
            //ScrollViewerExt.SetVerticalScrollButtonDownHandler(source);
            //ScrollViewerExt.SetVerticalPageButtonDownHandler(source);
            //ScrollViewerExt.SetHorizontalScrollButtonRightHandler(source);
            //ScrollViewerExt.SetHorizontalPageButtonRightHandler(source);

            //ScrollViewerExt.SetVerticalPageButtonUpHandler(source);
            //ScrollViewerExt.SetHorizontalScrollButtonLeftHandler(source);
            //ScrollViewerExt.SetHorizontalPageButtonLeftHandler(source);

            //ScrollViewerExt.SetThumbDragDeltaHandler(source, lbsr);
            //ScrollViewerExt.SetHorizThumbDragDeltaHandler(source, lbsr);

            return source;
        }

        #endregion

        #region Store Settings Specific To TextBoxBase
        
        public static TextBoxBase SetBackgroundColor(this TextBoxBase chain, Brush brush)
        {
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }

        public static TextBoxBase SetBackgroundColorGradient(this TextBoxBase chain, string gradientStop1Color, double gradientStop1Offset, string gradientStop2Color, double gradientStop2Offset, int startPointX, int startPointY, int endPointX, int endPointY)
        {
            LinearGradientBrush backgroundLinearBrush = BrushExt.CreateGradientBrush(gradientStop1Color, gradientStop1Offset, gradientStop2Color, gradientStop2Offset, startPointX, startPointY, endPointX, endPointY);

            chain.Background = backgroundLinearBrush;
            return chain;
        }

        public static TextBoxBase SetBackgroundColor(this TextBoxBase chain, byte red, byte green, byte blue, byte alpha)
        {
            Brush newBrush = (Brush)(new
System.Windows.Media.SolidColorBrush(new System.Windows.Media.Color() { R = red, G = green, B = blue, A = alpha }));
            chain.Background = newBrush;
            return chain;
        }


//        public static TextBoxBase SetSelectedContentControlColor(this TextBoxBase chain, Color color)
//        {
//            chain.Resources.Add(SystemColors.HighlightBrushKey, new SolidColorBrush(color));
//            chain.Resources.Add(SystemColors.ControlBrushKey, new SolidColorBrush(color));
//            return chain;
//        }
        
        #endregion

    }//end of class
}//end of namespace
